package laf.startup;

import jade.core.Profile;
import jade.core.ProfileImpl;
import jade.core.Runtime;
import jade.tools.rma.rma;
import jade.wrapper.AgentController;
import jade.wrapper.ContainerController;
import jade.wrapper.StaleProxyException;

import java.io.IOException;
import java.io.ObjectOutputStream;
import java.util.ArrayList;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;

import laf.agents.CognitiveAgent;
import laf.agents.LanguageSymbolDictionary;
import laf.behaviours.CollectGroundingHistory;
import laf.env.Attribute;
import laf.env.AttributeDomain;
import laf.env.AttributeValue;
import laf.env.EnvObject;
import laf.env.Enviroment;
import laf.env.State;
import laf.lang.Symbol;
import laf.learning.GroundingStrategyImpl;
import laf.learning.KESGroundingRule;
import laf.learning.LocalGroundingRule;
import laf.learning.ReactiveSymbolGroundingBehaviour;
import laf.learning.StochasticApproximationGroundingRule;
import laf.time.TimeManager;
import laf.ui.CorrelationChart;

public class AirStartUp {
	
	private static final long TICK_DURATION = 10000;

	public static void main(String[] args) {
		
		Logger.getLogger("laf").setLevel(Level.WARNING);
		TimeManager tm = TimeManager.getInstance();
		tm.setTickDuration(1000);
		Enviroment env = Enviroment.getInstance();
		
		AttributeDomain domain100 = Enviroment.createDomain("0-100", 0, 100);
		
		Attribute velocity = Enviroment.createAttribute("velocity", domain100);
		//Attribute height = Enviroment.createAttribute("height", domain100);
		Attribute direction = Enviroment.createAttribute("direction", domain100);
		
		for (int i = 1; i < 10; i++) {
			Enviroment.createObject("o" + i);
		}
		
		List<Symbol> symbols = new ArrayList<Symbol>();
		Symbol stoped = new Symbol("Stoped");
		symbols.add(stoped);
		Symbol lowSpeed = new Symbol("LowSpeed");
		symbols.add(lowSpeed);
		Symbol midSpeed = new Symbol("MidSpeed");
		symbols.add(midSpeed);
		Symbol hiSpeed = new Symbol("HiSpeed");
		symbols.add(hiSpeed);
		
		Symbol north = new Symbol("North");
		symbols.add(north);
		Symbol east = new Symbol("East");
		symbols.add(east);
		Symbol west = new Symbol("West");
		symbols.add(west);
		Symbol south = new Symbol("South");
		symbols.add(south);
		
		LanguageSymbolDictionary dictionary = new LanguageSymbolDictionary(symbols, env.getAttributes());
		LanguageSymbolDictionary.createCorrelation(dictionary, stoped, velocity.getValue(0), velocity.getValue(0), 1, Long.MAX_VALUE);
		LanguageSymbolDictionary.createCorrelation(dictionary, lowSpeed, velocity.getValue(1), velocity.getValue(34), 1, Long.MAX_VALUE);
		LanguageSymbolDictionary.createCorrelation(dictionary, midSpeed, velocity.getValue(35), velocity.getValue(67), 1, Long.MAX_VALUE);
		LanguageSymbolDictionary.createCorrelation(dictionary, hiSpeed, velocity.getValue(68), velocity.getValue(100), 1, Long.MAX_VALUE);
		
		LanguageSymbolDictionary.createCorrelation(dictionary, north, direction.getValue(0), direction.getValue(24), 1, Long.MAX_VALUE);
		LanguageSymbolDictionary.createCorrelation(dictionary, east, direction.getValue(25), direction.getValue(49), 1, Long.MAX_VALUE);
		LanguageSymbolDictionary.createCorrelation(dictionary, west, direction.getValue(50), direction.getValue(74), 1, Long.MAX_VALUE);
		LanguageSymbolDictionary.createCorrelation(dictionary, south, direction.getValue(75), direction.getValue(100), 1, Long.MAX_VALUE);
		
		LanguageSymbolDictionary copy = new LanguageSymbolDictionary(dictionary);
		
		Runtime jadeRT = Runtime.instance();
		Profile profile = new ProfileImpl();
		ContainerController containerCntr = jadeRT.createMainContainer(profile);
		try {
			Object[] arguments = { copy };
			AgentController agentCntr = containerCntr.createNewAgent("CA1", CognitiveAgent.class.getName(), arguments);
			agentCntr.start();
			
			CognitiveAgent newAgent = new CognitiveAgent();
			newAgent.addBehaviour(new ReactiveSymbolGroundingBehaviour(newAgent, new GroundingStrategyImpl(new LocalGroundingRule(0.1))));
			newAgent.addBehaviour(new CollectGroundingHistory(newAgent, 1, 9));
			agentCntr = containerCntr.acceptNewAgent("NA", newAgent);
			agentCntr.start();
			
			//agentCntr = containerCntr.createNewAgent("RMA", rma.class.getName(), null);
			//agentCntr.start();
			
			CorrelationChart chart = new CorrelationChart();
			chart.setGroundingHistory(newAgent.getGroundingHistory());
			chart.setVisible(true);
		} catch (StaleProxyException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		tm.start();
		
		//System.out.println(dictionary);
	}

}
